/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DiscountFrame.java
 *
 * Created on Mar 25, 2011, 2:05:55 PM
 */

package gbbmgmt;

import java.util.*;
import org.jdesktop.application.Action;
import javax.swing.JOptionPane;

/**
 *
 * @author dchapman
 */
public class DiscountFrame extends javax.swing.JFrame {

    public ArrayList<String[]> Discount;

    /** Creates new form DiscountFrame */
    public DiscountFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBoxDiscount = new javax.swing.JComboBox();
        jButtonCustFirst = new javax.swing.JButton();
        jButtonCustPrev = new javax.swing.JButton();
        jButtonCustNext = new javax.swing.JButton();
        jButtonCustLast = new javax.swing.JButton();
        jButtonSubmit = new javax.swing.JButton();
        jTextFieldDiscID = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldDiscName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldDiscNum = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jButtonModifyRecord = new javax.swing.JButton();
        jButtonNewRecord = new javax.swing.JButton();
        jButtonDeleteRecord = new javax.swing.JButton();
        jButtonExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jComboBoxDiscount.setName("jComboBoxDiscount"); // NOI18N
        jComboBoxDiscount.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBoxDiscountPopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getActionMap(DiscountFrame.class, this);
        jButtonCustFirst.setAction(actionMap.get("FirstRecord")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getResourceMap(DiscountFrame.class);
        jButtonCustFirst.setText(resourceMap.getString("jButtonCustFirst.text")); // NOI18N
        jButtonCustFirst.setName("jButtonCustFirst"); // NOI18N

        jButtonCustPrev.setAction(actionMap.get("PrevRecord")); // NOI18N
        jButtonCustPrev.setText(resourceMap.getString("jButtonCustPrev.text")); // NOI18N
        jButtonCustPrev.setName("jButtonCustPrev"); // NOI18N

        jButtonCustNext.setAction(actionMap.get("NextRecord")); // NOI18N
        jButtonCustNext.setText(resourceMap.getString("jButtonCustNext.text")); // NOI18N
        jButtonCustNext.setName("jButtonCustNext"); // NOI18N

        jButtonCustLast.setAction(actionMap.get("LastRecord")); // NOI18N
        jButtonCustLast.setText(resourceMap.getString("jButtonCustLast.text")); // NOI18N
        jButtonCustLast.setName("jButtonCustLast"); // NOI18N

        jButtonSubmit.setAction(actionMap.get("SubmitRecord")); // NOI18N
        jButtonSubmit.setText(resourceMap.getString("jButtonSubmit.text")); // NOI18N
        jButtonSubmit.setName("jButtonSubmit"); // NOI18N

        jTextFieldDiscID.setEditable(false);
        jTextFieldDiscID.setName("jTextFieldDiscID"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextFieldDiscName.setName("jTextFieldDiscName"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jTextFieldDiscNum.setName("jTextFieldDiscNum"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        jButtonModifyRecord.setAction(actionMap.get("ModifyRecord")); // NOI18N
        jButtonModifyRecord.setText(resourceMap.getString("jButtonModifyRecord.text")); // NOI18N
        jButtonModifyRecord.setName("jButtonModifyRecord"); // NOI18N

        jButtonNewRecord.setAction(actionMap.get("NewRecord")); // NOI18N
        jButtonNewRecord.setText(resourceMap.getString("jButtonNewRecord.text")); // NOI18N
        jButtonNewRecord.setName("jButtonNewRecord"); // NOI18N

        jButtonDeleteRecord.setAction(actionMap.get("DeleteRecord")); // NOI18N
        jButtonDeleteRecord.setText(resourceMap.getString("jButtonDeleteRecord.text")); // NOI18N
        jButtonDeleteRecord.setName("jButtonDeleteRecord"); // NOI18N

        jButtonExit.setAction(actionMap.get("Exit")); // NOI18N
        jButtonExit.setText(resourceMap.getString("jButtonExit.text")); // NOI18N
        jButtonExit.setName("jButtonExit"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextFieldDiscID, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 237, Short.MAX_VALUE)
                        .addComponent(jButtonSubmit))
                    .addComponent(jTextFieldDiscNum, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldDiscName, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonModifyRecord)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonNewRecord)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonDeleteRecord)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 143, Short.MAX_VALUE)
                .addComponent(jButtonExit)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jComboBoxDiscount, 0, 257, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCustFirst)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCustPrev)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCustNext)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCustLast)
                .addContainerGap(10, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jButtonCustPrev)
                    .addComponent(jButtonCustFirst)
                    .addComponent(jButtonCustNext)
                    .addComponent(jButtonCustLast)
                    .addComponent(jComboBoxDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSubmit)
                    .addComponent(jTextFieldDiscID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldDiscName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldDiscNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonModifyRecord)
                    .addComponent(jButtonExit)
                    .addComponent(jButtonNewRecord)
                    .addComponent(jButtonDeleteRecord))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxDiscountPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBoxDiscountPopupMenuWillBecomeInvisible
        // TODO add your handling code here:
        String Itm = jComboBoxDiscount.getSelectedItem().toString();

        for(String s[] : Discount){
            if (s[1].equals(Itm)) {
                DBA.openDBCon();
                DBA.GetDiscountCodeData(s[0]);
                DBA.closeDBCon();
            }
        }

        SetData();
}//GEN-LAST:event_jComboBoxDiscountPopupMenuWillBecomeInvisible

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DiscountFrame().setVisible(true);
            }
        });
    }

    public void LoadList(){
        DBA.openDBCon();
        Discount = DBA.RunSQLQuery("SELECT DiscID,DiscName FROM DiscountCode");
        DBA.closeDBCon();

        for(String s[] : Discount){
            jComboBoxDiscount.addItem(s[1]);
        }

        FirstRecord();
    }

    public void SetData(){
        jTextFieldDiscID.setText(Integer.toString(DBA.DiscID));
        jTextFieldDiscName.setText(DBA.DiscName);
        jTextFieldDiscNum.setText(Float.toString(DBA.DiscNum*100));

        jButtonSubmit.setVisible(false);
    }

    @Action
    public void FirstRecord() {
        jComboBoxDiscount.setSelectedIndex(0);

        String Itm = jComboBoxDiscount.getSelectedItem().toString();

        for(String s[] : Discount){
            if (s[1].equals(Itm)) {
                DBA.openDBCon();
                DBA.GetDiscountCodeData(s[0]);
                DBA.closeDBCon();
            }
        }

        SetData();
    }

    @Action
    public void LastRecord() {
        jComboBoxDiscount.setSelectedIndex(jComboBoxDiscount.getItemCount()-1);

        String Itm = jComboBoxDiscount.getSelectedItem().toString();

        for(String s[] : Discount){
            if (s[1].equals(Itm)) {
                DBA.openDBCon();
                DBA.GetDiscountCodeData(s[0]);
                DBA.closeDBCon();
            }
        }

        SetData();
    }

    @Action
    public void PrevRecord() {
        if(jComboBoxDiscount.getSelectedIndex()>=1){
            jComboBoxDiscount.setSelectedIndex(jComboBoxDiscount.getSelectedIndex()-1);
        }

        String Itm = jComboBoxDiscount.getSelectedItem().toString();

        for(String s[] : Discount){
            if (s[1].equals(Itm)) {
                DBA.openDBCon();
                DBA.GetDiscountCodeData(s[0]);
                DBA.closeDBCon();
            }
        }

        SetData();
    }

    @Action
    public void NextRecord() {
        if(jComboBoxDiscount.getSelectedIndex() <= (jComboBoxDiscount.getItemCount()-2)){
            jComboBoxDiscount.setSelectedIndex(jComboBoxDiscount.getSelectedIndex()+1);
        }

        String Itm = jComboBoxDiscount.getSelectedItem().toString();

        for(String s[] : Discount){
            if (s[1].equals(Itm)) {
                DBA.openDBCon();
                DBA.GetDiscountCodeData(s[0]);
                DBA.closeDBCon();
            }
        }

        SetData();
    }

    @Action
    public void ModifyRecord() {
        int response = JOptionPane.showConfirmDialog(null,
                "This will modify discount code data, continue?",
                "Warning",JOptionPane.WARNING_MESSAGE);

        if (response == 0){
            //User wants to update database, check for differences.
            DBA.openDBCon();

            Double number = (Double.valueOf(jTextFieldDiscNum.getText().trim()).doubleValue())/100;

            String DiscID = jTextFieldDiscID.getText();

            String query = "UPDATE DiscountCode SET DiscName='" +
                    jTextFieldDiscName.getText() + "',DiscNum='" +
                    Double.toString(number) + "' ";

            query += "WHERE DiscID='" + DiscID + "'";

            DBA.RunSQLUpdateQuery(query);

            DBA.GetDiscountCodeData(DiscID);

            DBA.closeDBCon();

            SetData();
        }
    }

    @Action
    public void NewRecord() {
        //Make all appropriate variables blank.
        jComboBoxDiscount.setVisible(false);
        jButtonCustFirst.setVisible(false);
        jButtonCustLast.setVisible(false);
        jButtonCustNext.setVisible(false);
        jButtonCustPrev.setVisible(false);
        jLabel1.setVisible(false);
        jTextFieldDiscID.setVisible(false);
        jButtonModifyRecord.setVisible(false);
        jButtonNewRecord.setVisible(false);
        jButtonDeleteRecord.setVisible(false);

        jButtonSubmit.setVisible(true);

        //reset text on all fields.
        jTextFieldDiscName.setText("");
        jTextFieldDiscNum.setText("0");
    }

    @Action
    public void DeleteRecord() {
        String query = "DELETE FROM DiscountCode WHERE DiscID = '"
                + jTextFieldDiscID.getText() + "'";
        int response = JOptionPane.showConfirmDialog(null,
                "Do you want to delete this entry from the Database?",
                "Warning",JOptionPane.WARNING_MESSAGE);
        if (response == 0){
            DBA.openDBCon();
            DBA.RunSQLUpdateQuery(query);
            DBA.closeDBCon();
            jComboBoxDiscount.removeAllItems();
            LoadList();
            LastRecord();
        }
    }

    @Action
    public void Exit() {
        dispose();
    }

    @Action
    public void SubmitRecord() {
        int response = JOptionPane.showConfirmDialog(null,
                "This will add new employee data, continue?",
                "Warning",JOptionPane.WARNING_MESSAGE);

        if (response == 0){
            Double number = (Double.valueOf(jTextFieldDiscNum.getText().trim()).doubleValue())/100;

            String query = "'" +
                jTextFieldDiscName.getText() + "','" +
                Double.toString(number) + "'";

            String fields = "DiscName,DiscNum";

            //Enter information into database.
            DBA.openDBCon();
            DBA.RunSQLInsertQuery("DiscountCode",fields,query);
            DBA.closeDBCon();

            jComboBoxDiscount.setVisible(true);
            jButtonCustFirst.setVisible(true);
            jButtonCustLast.setVisible(true);
            jButtonCustNext.setVisible(true);
            jButtonCustPrev.setVisible(true);
            jLabel1.setVisible(true);
            jTextFieldDiscID.setVisible(true);
            jButtonModifyRecord.setVisible(true);
            jButtonNewRecord.setVisible(true);
            jButtonDeleteRecord.setVisible(true);

            jButtonSubmit.setVisible(false);

            jComboBoxDiscount.removeAllItems();
            LoadList();
            LastRecord();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCustFirst;
    private javax.swing.JButton jButtonCustLast;
    private javax.swing.JButton jButtonCustNext;
    private javax.swing.JButton jButtonCustPrev;
    private javax.swing.JButton jButtonDeleteRecord;
    private javax.swing.JButton jButtonExit;
    private javax.swing.JButton jButtonModifyRecord;
    private javax.swing.JButton jButtonNewRecord;
    private javax.swing.JButton jButtonSubmit;
    private javax.swing.JComboBox jComboBoxDiscount;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField jTextFieldDiscID;
    private javax.swing.JTextField jTextFieldDiscName;
    private javax.swing.JTextField jTextFieldDiscNum;
    // End of variables declaration//GEN-END:variables

}
